<?php
namespace Home\Controller;
use Think\Controller;
use \Think\Page;
class UserController extends CommonController {
	public function code(){
		$Verify = new \Think\Verify();
		$Verify->fontSize =  20;    // 大小
		$Verify->length   =    4;    // 个数
		$Verify->useNoise =   false; // 有无点啥的
		$Verify->codeSet = '0123456789'; //设置为纯数字
		$Verify->entry();
	}
	
	
	// public function chakan(){
	// 	$Verify = new \Think\Verify();
	// 	$res = $Verify->check(I('post.code'));
	// 	if($res){
	// 		if(md5(I('post.repassword')) == $_SESSION['users']['repassword']){
	// 			$this -> display('home/user/teamPic');
	// 		}else{
	// 			$this -> error('安全码不正确');
	// 		}
	// 	}else{
	// 		$this -> error('验证码不正确');
	// 	}
		
	// }

	// public function tupu(){
	// 	$this -> display();
	// }

	//注册
	public function register(){
		// dump(I('get.'));die;
		$this -> assign('position',I('get.position'));
		$this -> assign('title','注册会员');
		$id = I('get.id');
		$mod = M('user_type');
		$type = $mod -> field('id,name,is_reg')->select();
		$this->assign('type',$type);
		$this -> assign('id',$id);
		$this -> display();
	}

	//判断手机号注册次数
	public function phoneAjax(){
		$mod = M('users');
		$res = $mod ->where('user_name="%s" ',I('post.user_name')) -> find(); 
		$num = $mod -> where('mobile_phone = "%s"',I('post.phone')) ->count();	
		if($num>10){
			echo '2';
		}else{
			if(I('post.mobile_code')==$_SESSION['phone']['code']){
				if($res>0){
					//yonghuming
					echo '222';
				}else{
					echo '1';
				}
			}else{
				echo '22';
			}
		}
	}

	//获取手机验证码
	public function mobile_phoneAjax(){
			//判断推荐人和报单中心是否存在
		$mod = M('users');
			$res1 = $mod -> where('user_id = "%s" ',I('post.inviteruser')) -> find(); 
			$res2 = $mod -> where('user_id = %d and is_reportcenter="%s" ',I('post.reportuser'),1) -> find(); 
			if($res1>0 && $res2>0){
				$_SESSION['phone']['code'] = rand('111111','999999');
								$_SESSION['phone']['mobile'] =  I('post.mobile_phone');
								$_SESSION['phone']['content']=",您的短信验证码为".$_SESSION['phone']['code']."，仅用于注册会员，短信验证码的有效时间为5分钟，使用后即作废";
							
							$this -> send_msg();


			}else{
				echo '22';
			}
				
	}

	//处理注册会员信息
	//验证信息
	public function doregister(){

		$user = I('post.');
		
		$tree = M('users') -> field('user_id,tree_code,tree_left_code,tree_right_code') -> where('user_id=%d',[$user['treeuser']]) -> find();
		// dump($tree);die;
		//加入安全随机码
		$user['ec_salt'] = rand('1111','9999');
		$user['report_user_id'] = $user['reportuser'];
		$user['tree_id'] = $user['treeuser'];
		$user['inviter_id'] = $user['inviteruser'];
		$user['password'] = md5(md5($user['password1']).$user['ec_salt']);
		$user['repassword'] = md5($user['repassword1']);
		$user['reg_time'] = time();
		$user['reg_type'] = $user['user_type'];
		$user['user_type'] = $user['user_type'];
		$user['huitian_time'] = time();
		if(empty($tree['tree_code'])){
			$user['tree_code'] = $tree['user_id'];
		}else{
			$user['tree_code'] = $tree['tree_code'].','.$user['tree_id'];
		}
		

		if($user['position'] == 'l'){
			if(empty($tree['tree_left_code'])){
				$user['tree_left_code'] = $tree['user_id'];
			}else{
				$user['tree_left_code'] = $tree['tree_left_code'].','.$user['tree_id'];
			}
			// dump($user['tree_left_code']);
			$user['tree_right_code'] = $tree['tree_right_code'];
		}elseif($user['position'] == 'r'){
			if(empty($tree['tree_right_code'])){
				$user['tree_right_code'] = $tree['user_id'];
			}else{
				$user['tree_right_code'] = $tree['tree_right_code'].','.$user['tree_id'];
			}
			$user['tree_left_code'] = $tree['tree_left_code'];
		}
		
		unset($user['password1']);unset($user['password2']);
		unset($user['repassword1']);unset($user['repassword2']);
		unset($user['reportuser']);unset($user['treeuser']);
		unset($user['inviteruser']);unset($user['mobile_code']);
		unset($user['position']);
		// dump($user);die;
		$mod = M('users');
		$res = $mod -> add($user);
		if($res>0){
			$this -> success('注册会员成功',U('home/user/teamPic'));
		}else{
			$this -> error('注册会员失败',U('home/user/register'));
		}

	}

	public function send_msg(){
				  
					//接口账号
					$uid = 'dong354698601';

					//登录密码
					$pwd = 'dong6395175346';

					/**
					* 实例化接口
					* 
					* @param string $uid 接口账号
					* @param string $pwd 接口密码					*/
				
					$api = new \Org\Util\Smsapi($uid,$pwd);

					$mobile = $_SESSION['phone']['mobile'];

					//短信内容参数
					$contentParam = array(
						'code'		=> $_SESSION['phone']['code'],
						'username'	=> $_SESSION['phone']['content']
						);

					//变量模板ID
					$template = '397816';

					//发送变量模板短信
					$result = $api->send($mobile,$contentParam,$template);

					if($result['stat']=='100')
					{
						echo '发送成功';
					}
					else
					{
						echo '发送失败:'.$result['stat'].'('.$result['message'].')';
					}

		}

	
	//激活
	public function jh(){
		$is = M('users') -> field('is_reportcenter') -> where('user_id=%d',[$_SESSION['users']['user_id']]) -> find();
		// dump($is);die;
		if($is['is_reportcenter'] == 1){
			$user = M('users') -> field('user_id,user_type,user_name,reg_time') -> where('report_user_id=%d and is_fh="%s"',[$_SESSION['users']['user_id'],1]) -> select();
			$count = M('users') -> where('report_user_id=%d and is_fh="%s"',[$_SESSION['users']['user_id'],1]) -> count();

			// dump($user);die;
			$this -> assign('count',$count);
			$this -> assign('user',$user);
			$this -> display();
		}else{
			
			$this -> error('对不起，你不是保单中心');
		}
	}

	//推荐图谱
	public function inviter(){
		$this -> display();
	}


	//团队图谱
	public function teamPic(){		
		$this -> assign('title','团队图谱');

		$mod = M('users') ;
		//判断用户搜索类型
		if(!empty(I('get.user_name'))){
			$user = $mod -> where('user_name = "%s" ',I('get.user_name')) ->find();
			$id = $user['user_id'];
		}else{
			if(empty(I('get.id'))){
				$id = $_SESSION['users']['user_id'];
				
			}else{
				$id = I('get.id');
			}
		}
		//传递当前最高等级
		$this -> assign('top_id',$_SESSION['users']['user_id']);
		$this -> assign('id',$id);
		
		$treeTop = $mod -> where('user_id = "%s" ',$id) -> find();
		$this -> assign('treeTop',$treeTop);
		//当前最高等级下的两人
		$treeMessage = $mod -> where('tree_id = "%s" ',$id) -> select();

		$res = strpos($treeMessage[0]['tree_left_code'],$id);
		$res2 = strpos($treeMessage[0]['tree_right_code'],$id);
		if($res>-1){
			$treeMessage0 = $treeMessage[0];
			$treeMessage1 = $treeMessage[1];
		}elseif($res2>-1){
			$treeMessage0 = $treeMessage[1];
			$treeMessage1 = $treeMessage[0];
		}

		$arr1 = $mod -> where('tree_id=%d',[$treeMessage0['user_id']]) -> select();
		// dump($arr1[0]);die;
		$res3 = strpos($arr1[0]['tree_left_code'],$treeMessage0['user_id']);
		$res4 = strpos($arr1[0]['tree_right_code'],$treeMessage0['user_id']);
		if($res3>-1){
			$treeMessage2 = $arr1[0];
			$treeMessage3 = $arr1[1];
		}elseif($res4>-1){
			$treeMessage2 = $arr1[1];
			$treeMessage3 = $arr1[0];
		}

		$arr2 = $mod -> where('tree_id=%d',[$treeMessage1['user_id']]) -> select();
		$res4 = strpos($arr2[0]['tree_left_code'],$treeMessage1['user_id']);
		$res5 = strpos($arr2[0]['tree_right_code'],$treeMessage1['user_id']);
		if($res4>-1){
			$treeMessage4 = $arr2[0];
			$treeMessage5 = $arr2[1];
		}elseif($res5>-1){
			$treeMessage4 = $arr2[1];
			$treeMessage5 = $arr2[0];
		}

		$this -> assign('treeMessage0',$treeMessage0);
		$this -> assign('treeMessage1',$treeMessage1);
		$this -> assign('treeMessage2',$treeMessage2);
		$this -> assign('treeMessage3',$treeMessage3);
		$this -> assign('treeMessage4',$treeMessage4);
		$this -> assign('treeMessage5',$treeMessage5);

		$this -> display();
	}

	//删除未激活用户
	public function delete(){
		$res1 = M('users') -> field('is_fh,tree_id,report_user_id') -> where('user_id=%d',[I('get.dd')]) -> find();
		if($res1['report_user_id'] == $_SESSION['users']['user_id']){
			if($res1['is_fh'] == 1){
				$res2 = M('users') -> where('tree_id=%d',[I('get.dd')]) -> count();
				if($res2>0){
					$this -> error('该用户已发展下线，不能删除');
				}else{
					$res3 = M('users') -> field('user_id') -> where('tree_id=%d',[$res1['tree_id']]) -> order('reg_time') -> select();
					$count = M('users') -> where('tree_id=%d',[$res1['tree_id']]) -> count();
					// dump($res3);die;
					// if($res3[0]['user_id'] == )
					if($count>1){
						if($res3[0]['user_id'] == I('get.dd')){
							$this -> error('右区已注册用户，不能删除');
						}else{
							$res4 = M('users') -> where('user_id=%d',[I('get.dd')]) -> delete();
							if($res4>0){
								$this -> success('删除成功');
							}else{
								$this -> error('删除失败');
							}
						}
					}else{
						$res4 = M('users') -> where('user_id=%d',[I('get.dd')]) -> delete();
						if($res4>0){
							$this -> success('删除成功');
						}else{
							$this -> error('删除失败');
						}
					}
				}
			}else{
				$this -> error('该用户已激活，不能删除');
			}
		}else{
			$this -> error('不是该用户操作');
		}
	}

	//判断手机号注册次数
	public function mobile_phone_num(){		
		$mod = M('users');
		
		$num = $mod -> where('mobile_phone = "%s"',I('post.mobile_phone')) ->count();	
		if($num>10){
			echo '2';
		}else{
			echo '1';
		}
	}







	//激活用户
// 	public function actjh(){
// 		// dump(I('post.'));die;
// 		if($_SESSION['users']['repassword'] == md5(I('post.repassword1'))){
// 			$param = M('param') -> field('param3') -> where('id=%d',[1]) -> find();
// 			$jd = M('user_type') -> field('jd_level') -> select();
// 			foreach($jd as $k=>$v){
// 				$arr[] = $v['jd_level'];
// 			}
// 			rsort($arr);
// 			$max = $arr[0];
// 			$res = M('users') -> field('user_id,tree_code,pay_amount,tree_id,inviter_id,tree_left_code,tree_right_code,reg_amount,zt_percent,jd_percent,jd_level') -> join('zx_user_type on zx_users.reg_type=zx_user_type.id') -> where('user_id=%d and user_name="%s" and report_user_id="%s"',[I('post.id1'),I('post.name1'),$_SESSION['users']['user_id']]) -> find();
// 			// echo M() -> _SQL();die;
// 			$res2 = M('users') -> field('user_money2') -> where('user_id=%d',[$_SESSION['users']['user_id']]) -> find();
// 			// echo M() -> _SQL();die;
// 			$inviter = M('users') -> field('zt_percent') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$res['inviter_id']]) -> find();

// 			if($res){
// 				if($res2['user_money2']<$res['reg_amount']){
// 					$this -> error('报单中心的积分注册积分不足');
// 				}else{
// 					//开启事务
// 					M() -> startTrans();
// 					//算左区的业绩
// 					if(!empty($res['tree_left_code'])){
// 						$left = M() -> execute('update zx_users set tree_num=tree_num+'.$res['reg_amount'].',left_num=left_num+'.$res['reg_amount'].',left_leave_amount=left_leave_amount+'.$res['reg_amount'].' where user_id in('.$res['tree_left_code'].')'); 
// 					}else{
// 						$left = true;
// 					}
// 					// dump($left);die;
// 					//算右区的业绩
// 					if(!empty($res['tree_right_code'])){
// 						$right = M() -> execute('update zx_users set tree_num=tree_num+'.$res['reg_amount'].',right_num=right_num+'.$res['reg_amount'].',right_leave_amount=right_leave_amount+'.$res['reg_amount'].' where user_id in('.$res['tree_right_code'].')');
// 						// echo M() -> _SQL();
// 					}else{
// 						$right = true;
// 					}
// 					//加入积分明细
// 					$data1 = ['user_id'=>$_SESSION['users']['user_id'],'user_money2'=>((-1)*($res['reg_amount'])),'change_type'=>5,'change_time'=>time(),'change_desc'=>'激活会员'.I('post.name1').'扣除'.$res['reg_amount'].'注册积分','about_user_name'=>I('post.id1')];
// 					$mingxi = M('account_log') -> data($data1) -> add();
// 					//保单中心扣除积分
// 					$baodan = M() -> execute('update zx_users set user_money2=user_money2-'.$res['reg_amount'].' where user_id='.$_SESSION['users']['user_id']);
// 					//销售奖
// 					if($res['inviter_id']==3000001){
// 						$xiaoshou=true;
// 					}else{
// 						$xsj = $res['reg_amount']*$inviter['zt_percent']/100;
// 						$xiaoshou = M() -> execute('update zx_users set pay_points1=pay_points1+'.$xsj.',user_money1=user_money1+'.$xsj.' where user_id='.$res['inviter_id']);
// 						$xsj1 = ['change_time'=>time(),'user_id'=>$res['inviter_id'],'pay_points1'=>$xsj,'user_money1'=>$xsj,'change_type'=>99,'change_desc'=>'销售奖：【激活'.I('post.name1').'会员ID：'.I('post.id1').'】'.$res['reg_amount'].'*'.$inviter['zt_percent'].'%='.$xsj,'about_user_name'=>I('post.id1')];
// 						M('account_log') -> data($xsj1) -> add();
// 					}
					
// 					//服务奖
// 					$fuwujiang = $res['reg_amount']*$param['param3']/100;
// 					$fuwujiang1 = ['change_time'=>time(),'user_id'=>$_SESSION['users']['user_id'],'user_money1'=>$fuwujiang,'change_type'=>99,'pay_points5'=>$fuwujiang,'change_desc'=>'服务奖：【激活：'.I('post.name1').'会员ID:'.I('post.id1').'】'.$res['reg_amount'].'*'.$param['param3'].'%='.$fuwujiang,'about_user_name'=>I('post.id1')];
// 					M('account_log') -> data($fuwujiang1) -> add();
// 					$fuwu = M() -> execute('update zx_users set pay_points5=pay_points5+'.$fuwujiang.',user_money1=user_money1+'.$fuwujiang.' where user_id='.$_SESSION['users']['user_id']);
// 					//平衡奖
// 					$dp = M() -> query('select tree_id,inviter_id,user_id,left_leave_amount,right_leave_amount,max_dp_amount,user_type from zx_users where user_id in('.$res['tree_code'].')');
// 					// echo M() -> _SQL();
// 					// dump($dp);
// 					// die;
// 					foreach($dp as $k=>$v){
// 						if($v['left_leave_amount']==0){
// 							$k = true;
// 							// $res5 = M() -> execute('update zx_users set left_leave_amount=0 where user_id='.$v['user_id']);
// 						}elseif($v['right_leave_amount']==0){
// 							$k = true;
// 							// $res5 = M() -> execute('update zx_users set right_leave_amount=0 where user_id='.$v['user_id']);
// 						}elseif($v['left_leave_amount']>=$v['right_leave_amount']){
// 							$jian = $v['left_leave_amount'] - $v['right_leave_amount'];

// 							$res5 = M() -> execute('update zx_users set left_leave_amount='.$jian.',right_leave_amount=0'.' where user_id='.$v['user_id']);
// 							// echo M()-> _SQL();
// 							$user_type = M('user_type') -> where('id=%d',[$v['user_type']]) -> find();
// 							//算出接点人要拿的平衡奖
// 							if($usertype == ''){
// 								$res6 = true;
// 							}else{
// 								$dpjx = $v['right_leave_amount']*$user_type['dp_percent']/100;
// 								$dpj = $dpjx + $v['max_dp_amount'];
// 								if($dpj>$user_type['dp_max_amount']){
// 									$dpj = $user_type['dp_max_amount'];
// 									$dpjx = $user_type['dp_max_amount']-$v['dp_max_aount'];
// 								}
// 								$res6 = M() -> execute('update zx_users set max_dp_amount='.$dpj.',user_money1=user_money1+'.$dpjx.',pay_points2=pay_points2+'.$dpjx.' where user_id='.$v['user_id']);
// 								$data2 = ['change_time'=>time(),'user_id'=>$v['user_id'],'user_money1'=>$dpjx,'pay_points2'=>$dpjx,'change_type'=>99,'change_desc'=>'平衡奖：【激活'.I('post.name1').'会员ID：'.I('post.id1').'】'.$v['right_leave_amount'].'*'.$user_type['dp_percent'].'%='.$dpjx,'about_user_name'=>I('post.id1')];
// 								M('account_log') -> data($data2) -> add();

// 							}
							

// 							$user = M('users') -> field('inviter_id,user_id,jd_level,jd_percent') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$v['inviter_id']]) -> find();
// 							// echo M()->_SQL();
// 							// dump($user);
// 							// echo $max;
// 							//计算代数奖
// 							for($i=1;$i<=$max;$i++){
// 								// echo $i;
// 								if(empty($user['user_id'])){
// 									$k = true;
// 								}elseif($user['user_id'] ==0){
// 									$k = true;
// 								}elseif($user['uesr_id']==3000001){
// 									$k = true;
// 								}else{
// 									if($i<=$user['jd_level']){
// 										$dsj = $dpjx*$user['jd_percent']/100;
// 										$res7 = M() -> execute('update zx_users set pay_points3=pay_points3+'.$dsj.',user_money1=user_money1+'.$dsj.' where user_id='.$user['user_id']);
// 										$data3 = ['change_time'=>time(),'user_id'=>$user['user_id'],'user_money1'=>$dsj,'pay_points3'=>$dsj,'change_type'=>99,'change_desc'=>'第'.$i.'代管理奖【激活'.I('post.name1').'会员ID：'.I('post.id1').'】'.$v['right_leave_amount'].'*'.$user_type['dp_percent'].'%*'.$user['jd_percent'].'%='.$dsj,'about_user_name'=>$v['user_id']];
// 										M('account_log') -> data($data3) -> add();

// 										$user = M('users') -> field('inviter_id,user_id,jd_percent,jd_level') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$user['inviter_id']]) -> find();
// 									}
// 									// echo 11111111;
// 								}

// 							}
// 						}elseif($v['left_leave_amount']<$v['right_leave_amount']){
// 							$jian = $v['right_leave_amount'] - $v['left_leave_amount'];
// 							$res5 = M() -> execute('update zx_users set right_leave_amount='.$jian.',left_leave_amount=0'.' where user_id='.$v['user_id']);
// 							// echo M() -> _SQL();
// 							$user_type = M('user_type') -> where('id=%d',[$v['user_type']]) -> find();
// 							//算出接点人要拿的平衡奖
// 							if($user_type==''){
// 								$res6 = true;
// 							}else{
// 								$dpjx = $v['left_leave_amount']*$user_type['dp_percent']/100;
// 								$dpj = $dpjx + $v['max_dp_amount'];
// 								if($dpj>$user_type['dp_max_amount']){
// 									$dpj = $user_type['dp_max_amount'];
// 									$dpjx = $user_type['dp_max_amount']-$v['dp_max_aount'];
// 								}
// 								$res6 = M() -> execute('update zx_users set max_dp_amount='.$dpj.',user_money1=user_money1+'.$dpjx.',pay_points2=pay_points2+'.$dpjx.' where user_id='.$v['user_id']);
// 								$data2 = ['change_time'=>time(),'user_id'=>$v['user_id'],'user_money1'=>$dpjx,'pay_points2'=>$dpjx,'change_type'=>99,'change_desc'=>'平衡奖：【激活'.I('post.name1').'会员ID：'.I('post.id1').'】'.$v['left_leave_amount'].'*'.$user_type['dp_percent'].'%='.$dpjx,'about_user_name'=>I('post.id1')];
// 								// dump($data2);
// 								M('account_log') -> data($data2) -> add();
// 							}
							
// 							// echo M() -> _SQL();die;
// 							// dump($res8);

// 							$user = M('users') -> field('inviter_id,user_id,jd_level,jd_percent') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$v['inviter_id']]) -> find();
// 							//计算代数奖
// 							for($i=1;$i<=$max;$i++){

// 								if(empty($user['user_id'])){
// 									$k = true;
// 								}elseif($user['user_id'] ==0){
// 									$k = true;
// 								}elseif($user['user_id']==3000001){
// 									$k = true;
// 								}else{
// 									if($i<=$user['jd_level']){
// 										$dsj = $dpjx*$user['jd_percent']/100;
// 										$res7 = M() -> execute('update zx_users set pay_points3=pay_points3+'.$dsj.',user_money1=user_money1+'.$dsj.' where user_id='.$user['user_id']);
// 										$data3 = ['change_time'=>time(),'user_id'=>$user['user_id'],'user_money1'=>$dsj,'pay_points3'=>$dsj,'change_type'=>99,'change_desc'=>'第'.$i.'代管理奖【激活'.I('post.name1').'会员ID：'.I('post.id1').'】'.$v['left_leave_amount'].'*'.$user_type['dp_percent'].'%*'.$user['jd_percent'].'%='.$dsj,'about_user_name'=>$v['user_id']];

// 										M('account_log') -> data($data3) -> add();
// 										// echo M() -> _SQL();die;
// 										$user = M('users') -> field('inviter_id,jd_percent,user_id,jd_percent,jd_level') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$user['inviter_id']]) -> find();
// 									}
// 									// echo 111;
// 								}
// 							}
// 						}

// 					}
// 					// echo 'aaaa';die;
// // dump();
// 					// dump($mingxi);
// 					// dump($baodan);
// 					// dump($xiaoshou);
// 					// dump($fuwu);
					
// 					//计算被激活用户购买的积分
// 					$buy_num =$res['reg_amount']*$res['pay_amount']/100;
// 					//更新日志
// 					$rizhi = M('account_log') -> data(['user_id'=>I('post.id1'),'user_money3'=>$buy_num,'change_type'=>60,'change_time'=>time(),'change_desc'=>'注册自动获得认购积分']) -> add();
// 					//激活的用户的认购积分
// 					$user1 = M() -> execute('update zx_users set user_money3=user_money3+'.$buy_num.',is_fh=0 where user_id='.I('post.id1'));
// 					// dump($buy_num);
// 					// dump($rizhi);
// 					// dump($user1);
// 					// dump($res5);
// 					// dump($res6);
// 					// dump($res7);
// 				// die;
// 					// echo 'jieshu';die;
// 					if($mingxi&&$baodan&&$xiaoshou&&$fuwu&&$buy_num&&$rizhi&&$user1){
// 						M() -> commit();
// 						$this -> success('激活成功');
// 					}else{
// 						M() -> rollback();
// 						$this -> error('激活失败');
// 					}
// 				}
// 			}else{
// 				$this -> error('填写的内容不匹配');
// 			}
// 		}else{
// 			$this -> error('安全码错误');
// 		}
// 	}
//激活用户
	public function actjh(){
		if($_SESSION['users']['repassword'] == md5(I('post.repassword1'))){
			$param = M('param') -> field('param3') -> where('id=%d',[1]) -> find();
			$jd = M('user_type') -> field('jd_level') -> select();
			foreach($jd as $k=>$v){
				$arr[] = $v['jd_level'];
			}
			rsort($arr);
			$max = $arr[0];
			$res = M('users') -> field('is_out,is_jh,user_id,tree_code,pay_amount,tree_id,inviter_id,tree_left_code,tree_right_code,reg_amount,zt_percent,jd_percent,jd_level') -> join('zx_user_type on zx_users.reg_type=zx_user_type.id') -> where('user_id=%d and user_name="%s" and report_user_id="%s"',[I('post.id1'),I('post.name1'),$_SESSION['users']['user_id']]) -> find();
			$res2 = M('users') -> field('user_money2') -> where('user_id=%d',[$_SESSION['users']['user_id']]) -> find();
			$inviter = M('users') -> field('zt_percent,is_out,is_jh') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$res['inviter_id']]) -> find();

			if($res){
				if($res2['user_money2']<$res['reg_amount']){
					$this -> error('报单中心的积分注册积分不足');
				}else{
					//开启事务
					M() -> startTrans();
					//算左区的业绩
					if(!empty($res['tree_left_code'])){
						$left = M() -> execute('update zx_users set tree_num=tree_num+'.$res['reg_amount'].',left_num=left_num+'.$res['reg_amount'].',left_leave_amount=left_leave_amount+'.$res['reg_amount'].' where  user_id in('.$res['tree_left_code'].')'); 
						
					}else{
						$left = true;
					}
					
					//算右区的业绩
					if(!empty($res['tree_right_code'])){
						$right = M() -> execute('update zx_users set tree_num=tree_num+'.$res['reg_amount'].',right_num=right_num+'.$res['reg_amount'].',right_leave_amount=right_leave_amount+'.$res['reg_amount'].' where  user_id in('.$res['tree_right_code'].')');
						
					}else{
						$right = true;
					}
					//加入积分明细
					$data1 = ['user_id'=>$_SESSION['users']['user_id'],'user_money2'=>((-1)*($res['reg_amount'])),'change_type'=>5,'change_time'=>time(),'change_desc'=>'激活会员'.I('post.name1').'扣除'.$res['reg_amount'].'注册积分','about_user_name'=>I('post.id1')];
					$mingxi = M('account_log') -> data($data1) -> add();
					//保单中心扣除积分
					$baodan = M() -> execute('update zx_users set user_money2=user_money2-'.$res['reg_amount'].' where user_id='.$_SESSION['users']['user_id']);
					//销售奖
					if($res['inviter_id']==3000001){
						$xiaoshou=true;
					}else{
						$xsj = $res['reg_amount']*$inviter['zt_percent']/100;
						$xiaoshou = M() -> execute('update zx_users set pay_points1=pay_points1+'.$xsj.',user_money1=user_money1+'.$xsj.' where user_id='.$res['inviter_id']);
						
						$xsj1 = ['change_time'=>time(),'user_id'=>$res['inviter_id'],'pay_points1'=>$xsj,'user_money1'=>$xsj,'change_type'=>99,'change_desc'=>'销售奖：【激活'.I('post.name1').'会员ID：'.I('post.id1').'】'.$res['reg_amount'].'*'.$inviter['zt_percent'].'%='.$xsj,'about_user_name'=>I('post.id1')];
						M('account_log') -> data($xsj1) -> add();
					}
					
					//服务奖
					
						$fuwujiang = $res['reg_amount']*$param['param3']/100;
						$fuwujiang1 = ['change_time'=>time(),'user_id'=>$_SESSION['users']['user_id'],'user_money1'=>$fuwujiang,'change_type'=>99,'pay_points5'=>$fuwujiang,'change_desc'=>'服务奖：【激活：'.I('post.name1').'会员ID:'.I('post.id1').'】'.$res['reg_amount'].'*'.$param['param3'].'%='.$fuwujiang,'about_user_name'=>I('post.id1')];
						M('account_log') -> data($fuwujiang1) -> add();
						// echo M() -> _SQL();
						$fuwu = M() -> execute('update zx_users set pay_points5=pay_points5+'.$fuwujiang.',user_money1=user_money1+'.$fuwujiang.' where user_id='.$_SESSION['users']['user_id']);
						// dump($fuwu);die;
					
					
					//平衡奖
					$dp = M() -> query('select tree_id,inviter_id,user_id,left_leave_amount,right_leave_amount,max_dp_amount,user_type from zx_users where  user_id in('.$res['tree_code'].')');
					// echo M() -> _SQL();
					// dump($dp);
					// die;
					foreach($dp as $k=>$v){
						if($v['left_leave_amount']==0){
							$k = true;
							// $res5 = M() -> execute('update zx_users set left_leave_amount=0 where user_id='.$v['user_id']);
						}elseif($v['right_leave_amount']==0){
							$k = true;
							// $res5 = M() -> execute('update zx_users set right_leave_amount=0 where user_id='.$v['user_id']);
						}elseif($v['left_leave_amount']>=$v['right_leave_amount']){
							$jian = $v['left_leave_amount'] - $v['right_leave_amount'];
							$res5 = M() -> execute('update zx_users set left_leave_amount='.$jian.',right_leave_amount=0'.' where user_id='.$v['user_id']);
							$user_type = M('user_type') -> where('id=%d',[$v['user_type']]) -> find();
							//算出接点人要拿的平衡奖
							if($user_type == ''){
								$res6 = true;
							}else{
								//当前应该获得的对碰奖
								$dpjx = $v['right_leave_amount']*$user_type['dp_percent']/100;
								//当日最大对碰 = 当前获得的对碰奖 + 今天获得的对碰奖
								$dpj = $dpjx + $v['max_dp_amount'];
								//如果加上的值大于该用户当日获得的最大
								if($dpj>=$user_type['dp_max_amount']){
									//将当前最大值赋给当日可获得最大值
									$dpj = $user_type['dp_max_amount'];
									//当前可获得的奖金 = 当日可获得最大值 - 当前用户已有的最大值
									$dpjx = $user_type['dp_max_amount']-$v['max_dp_amount'];
									$res6 = M() -> execute('update zx_users set max_dp_amount='.$dpj.',user_money1=user_money1+'.$dpjx.',pay_points2=pay_points2+'.$dpjx.' where user_id='.$v['user_id']);
							
									$data2 = ['change_time'=>time(),'user_id'=>$v['user_id'],'user_money1'=>$dpjx,'pay_points2'=>$dpjx,'change_type'=>99,'change_desc'=>'平衡奖：【激活'.I('post.name1').'会员ID：'.I('post.id1').'】,由于当日封顶,获得'.$dpjx,'about_user_name'=>I('post.id1')];
									M('account_log') -> data($data2) -> add();
								}else{
									$res6 = M() -> execute('update zx_users set max_dp_amount='.$dpj.',user_money1=user_money1+'.$dpjx.',pay_points2=pay_points2+'.$dpjx.' where user_id='.$v['user_id']);
									
									$data2 = ['change_time'=>time(),'user_id'=>$v['user_id'],'user_money1'=>$dpjx,'pay_points2'=>$dpjx,'change_type'=>99,'change_desc'=>'平衡奖：【激活'.I('post.name1').'会员ID：'.I('post.id1').'】'.$v['right_leave_amount'].'*'.$user_type['dp_percent'].'%='.$dpjx,'about_user_name'=>I('post.id1')];
									// dump($res6);
									M('account_log') -> data($data2) -> add();
								}

							}
							$user = M('users') -> field('inviter_id,user_id,jd_level,jd_percent') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$v['inviter_id']]) -> find();
							//计算代数奖
							for($i=1;$i<=$max;$i++){
								// echo $i;
								if(empty($user['user_id'])){
									$k = true;
								}elseif($user['user_id'] ==0){
									$k = true;
								}elseif($user['uesr_id']==3000001){
									$k = true;
								}else{
									if($i<=$user['jd_level']){
										$dsj = $dpjx*$user['jd_percent']/100;
										$res7 = M() -> execute('update zx_users set pay_points3=pay_points3+'.$dsj.',user_money1=user_money1+'.$dsj.' where user_id='.$user['user_id']);
										$data3 = ['change_time'=>time(),'user_id'=>$user['user_id'],'user_money1'=>$dsj,'pay_points3'=>$dsj,'change_type'=>99,'change_desc'=>'第'.$i.'代管理奖【激活'.I('post.name1').'会员ID：'.I('post.id1').'】'.$v['right_leave_amount'].'*'.$user_type['dp_percent'].'%*'.$user['jd_percent'].'%='.$dsj,'about_user_name'=>$v['user_id']];
										M('account_log') -> data($data3) -> add();
										$user = M('users') -> field('is_out,is_jh,inviter_id,user_id,jd_percent,jd_level') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$user['inviter_id']]) -> find();
									}
								}

							}
						}elseif($v['left_leave_amount']<$v['right_leave_amount']){
							$jian = $v['right_leave_amount'] - $v['left_leave_amount'];
							$res5 = M() -> execute('update zx_users set right_leave_amount='.$jian.',left_leave_amount=0'.' where user_id='.$v['user_id']);
							$user_type = M('user_type') -> where('id=%d',[$v['user_type']]) -> find();
							// $user_type1 = M('user_type') -> where('id=%d',[$v['reg_type']]) -> find();
							//算出接点人要拿的平衡奖
							if($user_type==''){
								$res6 = true;
							}else{
								$dpjx = $v['left_leave_amount']*$user_type['dp_percent']/100;
								$dpj = $dpjx + $v['max_dp_amount'];
								if($dpj>=$user_type['dp_max_amount']){
									$dpj = $user_type['dp_max_amount'];
									$dpjx = $user_type['dp_max_amount']-$v['max_dp_amount'];
									$res6 = M() -> execute('update zx_users set max_dp_amount='.$dpj.',user_money1=user_money1+'.$dpjx.',pay_points2=pay_points2+'.$dpjx.' where user_id='.$v['user_id']);
									
									$data2 = ['change_time'=>time(),'user_id'=>$v['user_id'],'user_money1'=>$dpjx,'pay_points2'=>$dpjx,'change_type'=>99,'change_desc'=>'平衡奖：【激活'.I('post.name1').'会员ID：'.I('post.id1').'】,由于当日封顶,获得'.$dpjx,'about_user_name'=>I('post.id1')];
									M('account_log') -> data($data2) -> add();
								}else{
									$res6 = M() -> execute('update zx_users set max_dp_amount='.$dpj.',user_money1=user_money1+'.$dpjx.',pay_points2=pay_points2+'.$dpjx.' where user_id='.$v['user_id']);
									
									$data2 = ['change_time'=>time(),'user_id'=>$v['user_id'],'user_money1'=>$dpjx,'pay_points2'=>$dpjx,'change_type'=>99,'change_desc'=>'平衡奖：【激活'.I('post.name1').'会员ID：'.I('post.id1').'】'.$v['left_leave_amount'].'*'.$user_type['dp_percent'].'%='.$dpjx,'about_user_name'=>I('post.id1')];
									// dump($res6);
									M('account_log') -> data($data2) -> add();
								}
								
							}
							// $res8 = M('account_log') -> data($data2) -> add();
							$user = M('users') -> field('inviter_id,user_id,jd_level,jd_percent') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$v['inviter_id']]) -> find();
							//计算代数奖
							for($i=1;$i<=$max;$i++){

								if(empty($user['user_id'])){
									$k = true;
								}elseif($user['user_id'] ==0){
									$k = true;
								}elseif($user['user_id']==3000001){
									$k = true;
								}else{
									if($i<=$user['jd_level']){
										$dsj = $dpjx*$user['jd_percent']/100;
										$res7 = M() -> execute('update zx_users set pay_points3=pay_points3+'.$dsj.',user_money1=user_money1+'.$dsj.' where user_id='.$user['user_id']);
										$data3 = ['change_time'=>time(),'user_id'=>$user['user_id'],'user_money1'=>$dsj,'pay_points3'=>$dsj,'change_type'=>99,'change_desc'=>'第'.$i.'代管理奖【激活'.I('post.name1').'会员ID：'.I('post.id1').'】'.$v['left_leave_amount'].'*'.$user_type['dp_percent'].'%*'.$user['jd_percent'].'%='.$dsj,'about_user_name'=>$v['user_id']];

										M('account_log') -> data($data3) -> add();
										// echo M() -> _SQL();die;
										$user = M('users') -> field('inviter_id,jd_percent,user_id,jd_percent,jd_level') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$user['inviter_id']]) -> find();
									}
									// echo 111;
								}
							}
						}

					}
					
					//计算被激活用户购买的积分
					$buy_num =$res['reg_amount']*$res['pay_amount']/100;
					//更新日志
					$rizhi = M('account_log') -> data(['user_id'=>I('post.id1'),'user_money3'=>$buy_num,'change_type'=>60,'change_time'=>time(),'change_desc'=>'注册自动获得认购积分']) -> add();
					//激活的用户的认购积分
					$user1 = M() -> execute('update zx_users set user_money3=user_money3+'.$buy_num.',is_fh=0 where user_id='.I('post.id1'));
					// echo 'jieshu';die;
					if($mingxi&&$baodan&&$xiaoshou&&$fuwu&&$buy_num&&$rizhi&&$user1){
						M() -> commit();
						$this -> success('激活成功');
					}else{
						M() -> rollback();
						$this -> error('激活失败');
					}
				}
			}else{
				$this -> error('填写的内容不匹配');
			}
		}else{
			$this -> error('安全码错误');
		}
	}

}